/**
 * 
 */
package com.surelution.tour.vo

import com.sun.tools.javac.code.Scope.ImportScope;

import com.surelution.tour.Department;
import com.surelution.tour.GasTank;
import com.surelution.tour.StockTaking;
import com.surelution.tour.GasUnloading;
/**
 * @author <a href="mailto:guangzong.syu@gmail.com">Guangzong</a>
 *
 */
class DepartmentPeriodStockTaking {

	Department department
	List<StockTaking> takings
	Date periodStart
	Date periodEnd
	
	public StockTaking getFirstTaking() {
		if(takings && takings.size() > 0)takings[0]
	}
	
	public StockTaking getLastTaking() {
		if(takings && takings.size() > 0) takings[takings.size() - 1]
	}
	
	public int getTakingCount() {
		(takings && takings.size() > 0) ? takings.size() : 0
	}
	
	/**
	 * 此处业务逻辑需要引起注意
	 * 在销量的处理中，需要将每个油罐的销售数量转化为v20，
	 * 而每个油罐的vt-v20的转换率是不一样的，所以，需要分别根据油罐来计算两次盘点间的油品销售泵码数的差值 X 转化率
	 * 这样才是真正的v20销售数
	 * @return 损耗率
	 */
	public Float getShrinkRate() {
		def gunsV20Amount = 0
		def unloadingAmount = 0
		def recircligAmount = 0
		def stockDiffer = 0
		def n = System.currentTimeMillis()
		tankTakings?.each {tankTaking ->
			gunsV20Amount += tankTaking.totalGunsAmount * tankTaking.vt2VcfRatio
			unloadingAmount += tankTaking.unloadingAmount
			recircligAmount += tankTaking.recirclingAmount
			def a = tankTaking.startTakingItem?.stockAmount
			def b  = tankTaking.endTakingItem?.stockAmount
			a = a?a:0
			b = b?b:0
			stockDiffer += (n>1356969600000)?0:(a - b)
		}
		if(gunsV20Amount != 0) {
			return (unloadingAmount + stockDiffer + recircligAmount - gunsV20Amount)/gunsV20Amount
		}
		else
			return 0
//		def startStockAmount = firstTaking?.totalStock
//		startStockAmount = startStockAmount?startStockAmount:0
//		
//		def endStockAmount = lastTaking?.totalStock
//		endStockAmount = endStockAmount?endStockAmount:0
//		
//		def startSalesAmount = firstTaking?.totalSalesAmount
//		startSalesAmount = startSalesAmount?startSalesAmount:0
//		
//		def endSalesAmount = lastTaking?.totalSalesAmount
//		endSalesAmount = endSalesAmount?endSalesAmount:0
//		
//		def totalUnloadingAmount = this.totalUnloadingAmount
//		totalUnloadingAmount = totalUnloadingAmount?totalUnloadingAmount:0
//		if(endSalesAmount - startSalesAmount != 0) {
//			println "${startStockAmount} ${endStockAmount} ${totalUnloadingAmount} ${endSalesAmount} ${startSalesAmount}"
//			return ((startStockAmount - endStockAmount) + totalUnloadingAmount - (endSalesAmount - startSalesAmount))/(endSalesAmount - startSalesAmount)
//		} else 
//			return 0
	}
	
	public Float getTotalUnloadingAmount() {
		def from = firstTaking?firstTaking.takingTime:periodStart
		def to = lastTaking?lastTaking.takingTime:periodEnd
		def unloadings = GasUnloading.findAllByDepartmentAndUnloadingTimeBetween(department, from, to)
		def amount = unloadings?.sum {
			it.amount
		}
		amount?amount:0
	}
	
	public List<TankPeriodStockTaking> getTankTakings() {
		def tanks = GasTank.findAllByDepartment(department)
		tanks?.collect {tank ->
			def firstStockTakingItem = firstTaking?.items?.find {item->
				item.tank.id == tank.id
			}
			def lastStockTakingItem = lastTaking?.items?.find {item->
				item.tank.id == tank.id
			}
			new TankPeriodStockTaking(tank:tank, 
				startTakingItem:firstStockTakingItem, endTakingItem:lastStockTakingItem)
		}
	}
	
}
